//
//  LackOfBalanceView.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/9.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "LackOfBalanceView.h"

@interface LackOfBalanceView ()

Copy void(^selectedHandle)();
Strong UIView *bgView;

@end

@implementation LackOfBalanceView

- (instancetype)initWithFrame:(CGRect)frame selectedHandle:(void (^)())selectedHandle{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        _selectedHandle = selectedHandle;
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        backButton.backgroundColor = [UIColor blackColor];
        backButton.alpha = .6;
        backButton.titleLabel.font = DEF_FontSize_13;
        [backButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        [self addSubview:self.bgView];
    }
    return self;
}

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(60 / WIDTH_5S_SCALE, 100 / WIDTH_5S_SCALE, SCREEN_WIDTH - 120 / WIDTH_5S_SCALE, 150 / WIDTH_5S_SCALE)];
        _bgView.backgroundColor = getColor(@"ffffff");
        _bgView.layer.masksToBounds = YES;
        _bgView.layer.cornerRadius = 5;
        UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 120 / WIDTH_5S_SCALE, 31 / WIDTH_5S_SCALE)];
        UILabel *titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 120 / WIDTH_5S_SCALE, 31 / WIDTH_5S_SCALE)];
        titleLabel.font = DEF_FontSize_14;
        titleLabel.textColor = getColor(@"ffffff");
        titleLabel.backgroundColor = getColor(mainColor);
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = @"会员卡支付";
        [headView addSubview:titleLabel];
        
        UILabel *title1Label  = [[UILabel alloc]initWithFrame:CGRectMake(20 / WIDTH_5S_SCALE, 50 / WIDTH_5S_SCALE, SCREEN_WIDTH - 120 / WIDTH_5S_SCALE - 40 / WIDTH_5S_SCALE, 50 / WIDTH_5S_SCALE)];
        title1Label.font = DEF_FontSize_14;
        title1Label.textColor = getColor(mainColor);
        title1Label.numberOfLines = 0;
        title1Label.text = @"您的会员卡余额不足,请去充值!";
        
        UIButton *typeButton = [[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 120 / WIDTH_5S_SCALE - 64 / WIDTH_5S_SCALE) / 2, 100 / WIDTH_5S_SCALE, 64 / WIDTH_5S_SCALE, 21 / WIDTH_5S_SCALE)];
        typeButton.layer.masksToBounds = YES;
        typeButton.layer.cornerRadius = 21 / WIDTH_5S_SCALE / 2;
        typeButton.titleLabel.font = DEF_FontSize_14;
        [typeButton setTitle:@"确定" forState:UIControlStateNormal];
        [typeButton setTitleColor:getColor(@"ffffff") forState:UIControlStateNormal];
        typeButton.backgroundColor = getColor(mainColor);
        [typeButton addTarget:self action:@selector(typeButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_bgView sd_addSubviews:@[headView ,typeButton , title1Label]];
    }
    return _bgView;
}

#pragma mark - click

- (void)typeButtonClick{
    [self dismiss];
    if (_selectedHandle) {
        _selectedHandle();
    }
}

//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
-(void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
